<template>
  <div class="app-container">
    <el-form label-position="left" :model="queryParams" ref="queryForm" size="mini" :inline="true">
      <el-form-item label="校区/学院/年级/专业班级" prop="areaCollegeGradeProClass">
        <el-cascader
          v-model="queryParams.areaCollegeGradeProClass"
          :options="stuClassCasList"
          :props="casProps"
          @change="casChange"></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['stuManage:stu:export']"
        >导出
        </el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="infoErrStus">
      <el-table-column label="学号" align="center" prop="stuNo" width="125"/>
      <el-table-column label="姓名" align="center" prop="name" width="70"/>
      <el-table-column label="学院" align="center" prop="params.college"/>
      <el-table-column label="年级" align="center" prop="params.grade" width="60"/>
      <el-table-column label="专业班级" align="center" prop="params.proClass"/>
      <el-table-column label="性别" align="center" width="50">
        <template slot-scope="scope">
          <span v-if="scope.row.sex===0">男</span>
          <span v-if="scope.row.sex===1">女</span>
        </template>
      </el-table-column>
      <el-table-column label="类别" align="center" width="65">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.stu_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="idNum"/>
      <el-table-column label="校区" align="center" width="65">
        <template slot-scope="scope">
          <span v-if="scope.row.schoolArea===1">新校区</span>
          <span v-if="scope.row.schoolArea===2">老校区</span>
        </template>
      </el-table-column>
      <el-table-column label="住宿信息" align="center" width="90">
        <template slot-scope="scope">
          <span>{{scope.row.params.buildingNo}}-{{scope.row.params.roomNo}}-{{scope.row.params.bedNo}}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phone" width="108"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.stu_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="现居地址" align="center" prop="addrNow" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" width="80" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="showUpdateStuDialog(scope.row)"
            v-hasPermi="['stuManage:stu:edit']"
          >
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="deleteStu(scope.row)"
            v-hasPermi="['stuManage:stu:remove']"
          >
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :close-on-click-modal="false" title="修改学生信息" :visible.sync="open" :fullscreen="true" append-to-body>
      <el-row :gutter="12">
        <el-col :span="10">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>基本信息</span>
            </div>
            <div class="text item">
              <el-form ref="form" :disabled="stuBaseInfoFormDisabled" :model="form" :rules="rules" label-width="70px"
                       size="medium">
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="姓名" prop="name">
                      <el-input v-model="form.name" placeholder="请输入姓名"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="性别" prop="sex">
                      <el-radio v-model="form.sex" :label="0">男</el-radio>
                      <el-radio v-model="form.sex" :label="1">女</el-radio>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="学历" prop="type">
                      <el-select v-model="form.type" placeholder="请选择学历">
                        <el-option
                          v-for="dict in dict.type.stu_type"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="状态" prop="status">
                      <el-select v-model="form.status" placeholder="请选择状态">
                        <el-option
                          v-for="dict in dict.type.stu_status"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        />
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="学号" prop="stuNo">
                      <el-input v-model="form.stuNo" placeholder="请输入学号"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="身份证号" prop="idNum">
                      <el-input v-model="form.idNum" placeholder="请输入身份证号"/>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="手机号" prop="phone">
                      <el-input v-model="form.phone" placeholder="请输入手机号"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="寝室长" prop="isAdminDor">
                      <el-radio v-model="form.isAdminDor" :label="1">是</el-radio>
                      <el-radio v-model="form.isAdminDor" :label="0">否</el-radio>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="党员" prop="isMember">
                      <el-radio v-model="form.isMember" :label="1">是</el-radio>
                      <el-radio v-model="form.isMember" :label="0">否</el-radio>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="团员" prop="isPartyMember">
                      <el-radio v-model="form.isPartyMember" :label="1">是</el-radio>
                      <el-radio v-model="form.isPartyMember" :label="0">否</el-radio>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-form-item label="有误" prop="isInfoErr">
                  <el-radio v-model="form.isInfoErr" :label="0">否</el-radio>
                  <el-radio v-model="form.isInfoErr" :label="1">是</el-radio>
                </el-form-item>
                <el-form-item label="现居地址" prop="addrNow">
                  <el-input v-model="form.addrNow" placeholder="请输入现居地址"/>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                  <el-input v-model="form.remark" placeholder="请输入备注"/>
                </el-form-item>
              </el-form>
            </div>
          </el-card>
        </el-col>
        <el-col :span="14">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>班级信息</span>
              <el-button style="float: right; padding: 3px 0" type="text" v-if="!stuClassInfoFormDisabled"
                         @click="showClassRelation">新增
              </el-button>
            </div>
            <div class="text item">
              <el-table v-loading="loading" :data="stuClassRelationList"
                        size="mini">
                <el-table-column label="校区" align="center" prop="params.schoolArea" width="70"/>
                <el-table-column label="学院" align="center" prop="params.college"/>
                <el-table-column label="年级" align="center" prop="params.grade" width="60"/>
                <el-table-column label="专业班级" align="center" prop="params.proClass" :show-overflow-tooltip="true"/>
                <el-table-column label="变动日期" align="center" prop="changeDateTime" width="110">
                  <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.changeDateTime, '{y}-{m}-{d}') }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="备注" align="center" prop="remark"/>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right"
                                 width="90">
                  <template slot-scope="scope">
                    <el-button
                      v-if="!stuClassInfoFormDisabled"
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="updateClassRelation(scope.row)"
                      v-hasPermi="['stuManage:stuClassRelation:edit']"
                    >
                    </el-button>
                    <el-button
                      v-if="!stuClassInfoFormDisabled"
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="deleteClassRelation(scope.row)"
                      v-hasPermi="['stuManage:stuClassRelation:remove']"
                    >
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
              <div style="margin-top: 5px" v-if="classCasShow">
                <el-form :model="classRelation" :inline="true" :disabled="stuClassInfoFormDisabled"
                         class="demo-form-inline">
                  <el-row>
                    <el-col :span="6">
                      <el-form-item label="">
                        <el-cascader
                          v-model="form.areaCollegeGradeProClass"
                          :options="stuClassCasList"
                          :props="casProps"
                          placeholder="班级"
                        ></el-cascader>
                      </el-form-item>
                    </el-col>
                    <el-col :span="7">
                      <el-form-item label="">
                        <el-date-picker
                          v-model="classRelation.changeDateTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="变更日期"
                        >
                        </el-date-picker>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="">
                        <el-input v-model="classRelation.remark" placeholder="备注"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="5">
                      <el-form-item>
                        <el-button type="primary" size="mini" @click="addClassRelation">确认</el-button>
                        <el-button type="warning" size="mini" @click="cancelAddClassRelation">取消</el-button>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </div>
          </el-card>
          <el-card style="margin-top: 5px" class="box-card">
            <div slot="header" class="clearfix">
              <span>宿舍信息</span>
              <el-button @click="dorCasShow=true" style="float: right; padding: 3px 0" type="text">新增
              </el-button>
            </div>
            <div class="text item">
              <el-table v-loading="loading" :data="stuDorRelationList"
                        size="mini">
                <el-table-column label="校区" align="center" prop="params.schoolArea" width="70"/>
                <el-table-column label="楼栋" align="center" prop="params.buildingNo" width="70"/>
                <el-table-column label="房间" align="center" prop="params.roomNo" width="70"/>
                <el-table-column label="床位" align="center" prop="params.bedNo" width="70"/>
                <el-table-column label="变动日期" align="center" prop="remark" width="110">
                  <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.changeDateTime, '{y}-{m}-{d}') }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="备注" align="center" prop="remark"/>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right"
                                 width="90">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="updateDorRelation(scope.row)"
                      v-hasPermi="['stuManage:stuDorRelation:edit']"
                    >
                    </el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="deleteDorRelation(scope.row)"
                      v-hasPermi="['stuManage:stuDorRelation:remove']"
                    >
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
              <div style="margin-top: 5px" v-if="dorCasShow">
                <el-form :inline="true" :model="dorRelation" :disabled="stuDorInfoFormDisabled"
                         class="demo-form-inline">
                  <el-row>
                    <el-col :span="6">
                      <el-form-item label="">
                        <el-cascader
                          v-model="form.areaBuildingFloorRoomBed"
                          :options="stuDorCasList"
                          placeholder="宿舍"
                          :props="{label:'label',value:'id',checkStrictly: true}"></el-cascader>
                      </el-form-item>
                    </el-col>
                    <el-col :span="5">
                      <el-form-item label="">
                        <el-date-picker
                          style="width: 160px"
                          v-model="dorRelation.changeDateTime"
                          type="date"
                          placeholder="变更日期"
                          value-format="yyyy-MM-dd"
                        >
                        </el-date-picker>
                      </el-form-item>
                    </el-col>
                    <el-col :span="9">
                      <el-form-item label="">
                        <el-input style="width: 300px" v-model="dorRelation.remark" placeholder="备注"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="4">
                      <el-form-item>
                        <el-button type="primary" size="mini" @click="addDorRelation">确认</el-button>
                        <el-button type="warning" size="mini" @click="cancelAddDorRelation">取消</el-button>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </div>
          </el-card>
          <el-card style="margin-top: 5px" class="box-card">
            <div slot="header" class="clearfix">
              <span>请假信息</span>
              <el-button @click="leaveShow=true" style="float: right; padding: 3px 0" type="text">新增</el-button>
            </div>
            <div class="text item">
              <el-table :data="leaveList">
                <el-table-column label="预计离校时间" align="center" prop="expectLeaveTime" width="110">
                  <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.expectLeaveTime, '{y}-{m}-{d}') }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="预计返校时间" align="center" prop="expectReturnTime" width="110">
                  <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.expectReturnTime, '{y}-{m}-{d}') }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="实际返校时间" align="center" prop="realReturnTime" width="110">
                  <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.realReturnTime, '{y}-{m}-{d}') }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="原因" align="center" prop="reason"/>
                <el-table-column label="目的地" align="center" prop="destination"/>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="90">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="updateLeave(scope.row)"
                    >
                    </el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="deleteLeave(scope.row)"
                    >
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
              <div style="margin-top: 5px" v-if="leaveShow">
                <el-form :model="stuLeave" size="mini"
                         class="demo-form-inline">
                  <el-row :gutter="8">
                    <el-col :span="4">
                      <el-form-item label="" prop="expectLeaveTime">
                        <el-date-picker clearable
                                        style="width: 135px"
                                        v-model="stuLeave.expectLeaveTime"
                                        type="date"
                                        value-format="yyyy-MM-dd"
                                        :picker-options="pickerBeginOption"
                                        placeholder="预计离校时间">
                        </el-date-picker>
                      </el-form-item>
                    </el-col>
                    <el-col :span="4">
                      <el-form-item label="" prop="expectReturnTime">
                        <el-date-picker clearable
                                        style="width: 135px"
                                        v-model="stuLeave.expectReturnTime"
                                        type="date"
                                        :picker-options="pickerBeginOption"
                                        value-format="yyyy-MM-dd"
                                        placeholder="预计返校时间"
                        >
                        </el-date-picker>
                      </el-form-item>
                    </el-col>
                    <el-col :span="4">
                      <el-form-item label="" prop="realReturnTime">
                        <el-date-picker clearable
                                        style="width: 135px"
                                        v-model="stuLeave.realReturnTime"
                                        type="date"
                                        :picker-options="pickerBeginOption"
                                        value-format="yyyy-MM-dd"
                                        placeholder="实际返校时间"
                        >
                        </el-date-picker>
                      </el-form-item>
                    </el-col>
                    <el-col :span="4">
                      <el-form-item label="" prop="reason">
                        <el-input v-model="stuLeave.reason" placeholder="原因"/>
                      </el-form-item>
                    </el-col>
                    <el-col :span="4">
                      <el-form-item label="" prop="destination">
                        <el-input v-model="stuLeave.destination" placeholder="目的地"/>
                      </el-form-item>
                    </el-col>
                    <el-col :span="4">
                      <el-form-item>
                        <el-button type="primary" @click="addLeave">确认</el-button>
                        <el-button type="warning" @click="cancelAddLeave">取消</el-button>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getInfoErrStus"
    />
  </div>
</template>

<script>
  import {findChildrenStuClassCasById} from "@/api/stu/class";
  import {listStu, getStu, delStu, addStu, updateStu} from "@/api/stu/stu";
  import {
    listStuClassRelation,
    getStuClassRelationByStuId,
    getStuClassRelation,
    updateStuLatestClassRelation,
    delStuClassRelation,
    addStuClassRelation,
    updateStuClassRelation
  } from "@/api/stu/stuClassRelation";
  import {
    listStuDorRelation,
    getStuDorRelation,
    updateStuLatestDorRelation,
    delStuDorRelation,
    addStuDorRelation,
    updateStuDorRelation
  } from "@/api/stu/stuDorRelation";
  import {listLeave, getLeave, delLeave, addLeave, updateLeave} from "@/api/stu/leave";
  import {findChildrenDorCasById, findChildrenDorCasByIds, findAreaBuildingFloorRoom} from "@/api/stu/dor";

  export default {
    name: "index",
    dicts: ['stu_status', 'stu_type'],
    data() {
      return {
        pickerBeginOption: {
          disabledDate: (time) => {
            return time.getTime() < Date.now() - 1 * 24 * 60 * 60 * 1000
          }
        },
        loading: false,
        total: 0,
        queryParams: {
          schoolAreaIds: [1, 2],
          isInfoErr: 1,
          pageNum: 1,
          pageSize: 10
        },
        classRelationParams:{},
        dorRelationParams:{},
        leaveParams:{},
        stuClassCasList: [],
        stuDorCasList:[],
        infoErrStus: [],
        casProps: {
          label: 'label',
          value: 'id',
          checkStrictly: true
        },
        open:false,
        dorCasShow:false,
        stuBaseInfoFormDisabled:false,
        stuClassInfoFormDisabled:false,
        stuDorInfoFormDisabled:false,
        form:{
          areaCollegeGradeProClass:[],
          areaBuildingFloorRoomBed:[],
        },
        rules:{},
        stuClassRelationList:[],
        stuDorRelationList:[],
        classRelation:{},
        stuClassRelationId:1,
        stuDorRelationId:1,
        dorRelation:{},
        classCasShow:false,
        leaveShow:false,
        leaveList:[],
        stuLeave:{}
      }
    },
    created() {
      this.getStuClassCasList()
      this.getStuDorCas()
      this.getInfoErrStus()
    },
    methods: {
      casChange() {

      },
      getStuClassCasList() {
        findChildrenStuClassCasById({parentId: 0}).then(res => {
          this.stuClassCasList = res.data
        })
      },
      getStuDorCas() {
        findChildrenDorCasById({parentId: 0}).then(res => {
          this.stuDorCasList = res.data
        })
      },
      getInfoErrStus() {
        this.loading = true;
        listStu(this.queryParams).then(res => {
          this.infoErrStus = res.rows;
          this.total = res.total;
          this.loading = false;
        });
      },
      getStuClassRelationList() {
        this.classRelationParams.stuId = this.form.id
        listStuClassRelation(this.classRelationParams).then(response => {
          this.stuClassRelationList = response.rows;
        });
      },
      // 学生宿舍关联表格数据
      getStuDorRelationList() {
        // this.stuDorRelationLoading = true;
        this.dorRelationParams.stuId = this.form.id
        listStuDorRelation(this.dorRelationParams).then(response => {
          this.stuDorRelationList = response.rows;
          // this.stuDorRelationLoading = false;
        });
      },
      getStuLeaveList() {
        this.leaveParams.stuId = this.form.id
        listLeave(this.leaveParams).then(response => {
          this.leaveList = response.rows;
        });
      },
      showClassRelation(){
        this.form.areaCollegeGradeProClass = []
        this.classCasShow = true
      },
      addClassRelation(){
        this.classRelation.areaCollegeGradeProClass = this.form.areaCollegeGradeProClass
        this.classRelation.stuId = this.form.id
        // 修改学生班级关联信息
        if (this.classRelation.stuId) {
          if (this.classRelation.id) {
            updateStuClassRelation(this.classRelation).then(res => {
              this.getStuClassRelationList()
              updateStuLatestClassRelation(this.form.id)
            })
          } else {
            addStuClassRelation(this.classRelation).then(res => {
              this.getStuClassRelationList()
              updateStuLatestClassRelation(this.form.id)
            })
          }
        } else {
          if (this.classRelation.id) {
            this.classRelationList.forEach(item => {
              if (item.id === this.classRelation.id) {
                item = this.classRelation
              }
            })
          } else {
            this.classRelation.id = this.stuClassRelationId++
            this.stuClassRelationList.push(this.classRelation)
          }
        }
        this.cancelAddClassRelation()
      },
      cancelAddClassRelation(){
        this.form.areaCollegeGradeProClass = []
        this.classRelation = {}
        this.classCasShow = false
      },
      updateClassRelation(row){
        if (row.stuId) {
          getStuClassRelation(row.id).then(res => {
            this.form.areaCollegeGradeProClass = res.data.areaCollegeGradeProClass
            this.classRelation = res.data
          })
        } else {
          this.classRelation = row
          this.form.areaCollegeGradeProClass = row.areaCollegeGradeProClass
        }
        this.classCasShow = true
      },
      deleteClassRelation(row){
        let stuId = this.form.id
        if (stuId) {
          this.$modal.confirm('确认删除？').then(function () {
            return delStuClassRelation(row.id);
          }).then(() => {
            this.getStuClassRelationList();
            this.$modal.msgSuccess("删除成功");
          }).catch(() => {
          });
        } else {
          this.$modal.confirm('确认删除？').then(function () {
          }).then(() => {
            this.leaveList = this.leaveList.filter(item => item.id !== row.id)
          }).catch(() => {
          });
        }
      },
      addDorRelation(){
        this.dorRelation.areaBuildingFloorRoomBed = this.form.areaBuildingFloorRoomBed
        this.dorRelation.stuId = this.form.id
        if (this.dorRelation.stuId) {
          // 修改
          if (this.dorRelation.id) {
            updateStuDorRelation(this.dorRelation).then(res => {
              this.getStuDorRelationList()
              updateStuLatestDorRelation(this.form.id)
            })
          } else {
            addStuDorRelation(this.dorRelation).then(res => {
              this.getStuDorRelationList()
              updateStuLatestDorRelation(this.form.id)
            })
          }
        } else {
          if (this.dorRelation.id) {
            this.stuDorRelationList.forEach(item => {
              if (item.id === this.dorRelation.id) {
                item = this.dorRelation
              }
            })
          } else {
            this.dorRelation.id = this.stuDorRelationId++
            this.stuDorRelationList.push(this.dorRelation)
          }
        }
        this.cancelAddDorRelation()
      },
      cancelAddDorRelation(){
        this.form.areaBuildingFloorRoomBed = []
        this.dorRelation = {}
        this.dorCasShow = false
      },
      updateDorRelation(row){
        if (row.stuId) {
          getStuDorRelation(row.id).then(res => {
            this.form.areaBuildingFloorRoomBed = res.data.areaBuildingFloorRoomBed
            this.dorRelation = res.data
          })
        } else {
          this.dorRelation = row
          this.form.areaBuildingFloorRoomBed = row.areaBuildingFloorRoomBed
        }
        this.dorCasShow = true
      },
      deleteDorRelation(row){
        let stuId = this.form.id
        if (stuId) {
          this.$modal.confirm('确认删除吗？').then(function () {
            return delStuDorRelation(row.id);
          }).then(() => {
            this.$modal.msgSuccess("删除成功");
            this.getStuDorRelationList();
          }).catch(() => {
          });
        } else {
          this.$modal.confirm('确认删除吗？').then(function () {
          }).then(() => {
            this.stuDorRelationList = this.stuDorRelationList.filter(item => item.id !== row.id)
          }).catch(() => {
          });
        }
      },
      addLeave(){
        let stuId = this.form.id
        if (stuId) {
          if (this.stuLeave.id) {
            updateLeave(this.stuLeave).then(res => {
              if (res.code === 200) {
                this.$message.success("修改学生请假记录成功！")
                this.getStuLeaveList()
                this.cancelAddLeave()
              }
            })
          } else {
            this.stuLeave.stuId = stuId
            addLeave(this.stuLeave).then(res => {
              if (res.code === 200) {
                this.$message.success("添加学生请假记录成功！")
                this.getStuLeaveList()
                this.cancelAddLeave()
              }
            })
          }
        } else {
          if (this.stuLeave.id) {
            this.leaveList.forEach(item => {
              if (item.id === this.stuLeave.id) {
                item = this.stuLeave
              }
            })
          } else {
            this.stuLeave.id = this.stuLeaveId++
            this.leaveList.push(this.stuLeave)
          }
          this.cancelAddLeave()
        }
      },
      cancelAddLeave(){
        this.stuLeave = {}
        this.leaveShow = false
      },
      updateLeave(row){
        if (row.stuId) {
          getLeave(row.id).then(res => {
            this.stuLeave = res.data
          })
        } else {
          this.stuLeave = row
        }
        this.leaveShow = true
      },
      deleteLeave(row){
        let stuId = this.form.id
        if (stuId) {
          this.$modal.confirm('确认删除？').then(function () {
            return delLeave(row.id);
          }).then(() => {
            this.getStuLeaveList();
            this.$modal.msgSuccess("删除成功");
          }).catch(() => {
          });
        } else {
          this.$modal.confirm('确认删除？').then(function () {
          }).then(() => {
            this.leaveList = this.leaveList.filter(item => item.id !== row.id)
          }).catch(() => {
          });
        }
      },
      // 表单重置
      reset() {
        this.form = {};
        this.stuClassRelationList = []
        this.stuDorRelationList = []
        this.resetForm("form");
        this.cancelAddDorRelation()
        this.cancelAddClassRelation()
        this.cancelAddLeave()
      },
      submitForm(){
        this.form.stuDorRelationList = this.stuDorRelationList
        this.form.stuClassRelationList = this.stuClassRelationList
        this.form.stuLeaveList = this.leaveList
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              this.form.schoolArea = null

              this.form.buildingId = null
              this.form.floorId = null
              this.form.roomId = null
              this.form.bedId = null

              this.form.collegeId = null
              this.form.gradeId = null
              this.form.proClassId = null

              updateStu(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.$tab.refreshPage()
              });
            } else {
              addStu(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.$tab.refreshPage()
              });
            }
          }
        });
      },
      cancel(){
        this.open = false;
        this.reset();
      },
      showUpdateStuDialog(row){
        this.reset();
        const id = row.id || this.ids
        getStu(id).then(response => {
          this.form = response.data;
          this.getStuClassRelationList()
          this.getStuDorRelationList()
          this.getStuLeaveList()
          this.form.type = this.form.type + ''
          this.form.status = this.form.status + ''
          this.open = true;
        });
      },
      deleteStu(row){
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除学生基本信息编号为"' + ids + '"的数据项？').then(function () {
          return delStu(ids);
        }).then(() => {
          this.getInfoErrStus();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      handleQuery() {
        this.getInfoErrStus()
      },
      resetQuery() {
        this.$tab.refreshPage()
      },
      handleExport() {
        this.download('stuManage/stu/export', {
          ...this.queryParams
        }, `stu_${new Date().getTime()}.xlsx`)
      },
    }
  }
</script>

<style>
  .el-dialog__body {
    padding: 0 5px;
  }
</style>
